package pl.alef.web;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.perf4j.aop.Profiled;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import pl.alef.mindplay.model.Comment;
import pl.alef.mindplay.model.Visit;
import pl.alef.mindplay.services.CommentService;
import pl.alef.mindplay.services.VisitService;

@Controller
public class AboutController {

	@Autowired
	protected CommentService commentService;
	
	protected Logger logger = LoggerFactory.getLogger(AboutController.class);
	
	List<Comment> list = new ArrayList<Comment>();
	
	@Profiled(tag="web:about:about")
	@RequestMapping("/about")
	public ModelAndView index(HttpServletRequest request) {
		logger.info("welocome at about 2");
		
		ModelAndView mv = new ModelAndView("about");
		boolean go = false;
		List<Comment> comments = commentService.list();
		logger.info("comments : " + comments);
		mv.addObject("comments", comments);		
		mv.addObject("comments2", list);
			
		Comment comment = new Comment();
		mv.addObject("comment", comment);
		
		logger.info("go to : " + mv);
		return mv;
	}
	
	 @RequestMapping(value="/about/addcomment")
	 public String saveComment(final Comment comment, final BindingResult bindingResult, final ModelMap model) {
		 logger.info("saveComment: " + comment);
		 logger.info("saveComment: " + bindingResult);
		 logger.info("saveComment: " + model);
		 commentService.save(comment);
		 list.add(comment);
		 model.clear();
		 return "redirect:/about.html";
	 }
	
}
